<template>
  <div class="scifi-stall-container">
    <!-- 动态背景元素 -->
    <div class="scifi-bg-elements">
      <div class="floating-orb orb-1"></div>
      <div class="floating-orb orb-2"></div>
      <div class="floating-orb orb-3"></div>
      <div class="grid-lines"></div>
    </div>

    <div class="scifi-content-wrapper">
      <div class="scifi-card">
        <!-- 标题区域 -->
        <div class="scifi-header">
          <div class="logo-container">
            <div class="hologram-logo">
              <div class="logo-core"></div>
              <div class="logo-ring"></div>
            </div>
          </div>
          <h1 class="scifi-title">公告通知管理</h1>
          <p class="scifi-subtitle">智能停车管理系统</p>
        </div>

        <!-- 搜索区域 -->
        <div class="scifi-search-panel">
          <div class="panel-header">
            <i class="iconfont icon-notice-menu"></i>
            <span>公告搜索</span>
          </div>
          <div class="search-controls">
            <div class="search-row">
              <div class="search-group">
                <div class="input-icon">
                  <i class="iconfont icon-stall-menu"></i>
                </div>
                <div class="scifi-input">
                  <el-input
                      placeholder="请输入公告标题"
                      v-model="queryInfo.title"
                      clearable
                  ></el-input>
                </div>
              </div>
              <div class="search-group">
                <el-button
                    type="primary"
                    class="scifi-btn search-btn"
                    @click="getList"
                >
                  <div class="btn-content">
                    <i class="iconfont icon-search-menu"></i>
                    <span>搜索</span>
                  </div>
                  <div class="btn-glow"></div>
                </el-button>
              </div>
              <div class="search-group">
                <el-button
                    type="primary"
                    class="scifi-btn order-btn"
                    @click="addNotice"
                >
                  <div class="btn-content">
                    <i class="iconfont icon-add-button"></i>
                    <span>新增</span>
                  </div>
                  <div class="btn-glow"></div>
                </el-button>
              </div>
            </div>
          </div>
        </div>

        <div class="scifi-divider"></div>

        <!-- 公告信息区域 -->
        <div class="scifi-table-panel">
          <div class="panel-header">
            <i class="iconfont icon-notice-menu"></i>
            <span>公告信息</span>
          </div>

          <div class="scifi-table-container">
            <div class="table-header">
              <div class="header-item">序号</div>
              <div class="header-item">公告标题</div>
              <div class="header-item">公告内容</div>
              <div class="header-item">创建时间</div>
              <div class="header-item">操作人</div>
              <div class="header-item">操作</div>
            </div>

            <div v-if="notices.length === 0" class="no-data">
              <div class="no-data-text">没有相关记录</div>
            </div>

            <div v-else class="table-body">
              <div v-for="(notice, index) in notices" :key="notice.id" class="table-row">
                <div class="table-cell">
                  <span class="index">{{ index + 1 }}</span>
                </div>
                <div class="table-cell">
                  <span class="title">{{ notice.title }}</span>
                </div>
                <div class="table-cell">
                  <span class="content" v-html="notice.content"></span>
                </div>
                <div class="table-cell">
                  <span class="create-time">{{ dateFormat(notice.createTime, 3) }}</span>
                </div>
                <div class="table-cell">
                  <span class="operator">{{ notice.operator }}</span>
                </div>
                <div class="table-cell action-cell">
                  <el-button
                      @click="toEdit(notice)"
                      class="scifi-btn action-btn edit-btn"
                  >
                    <div class="btn-content">
                      <i class="iconfont icon-edit-button"></i>
                      <span>编辑</span>
                    </div>
                    <div class="btn-glow"></div>
                  </el-button>
                  <el-button
                      @click="noticeDelete(notice.id)"
                      class="scifi-btn action-btn delete-btn"
                  >
                    <div class="btn-content">
                      <i class="iconfont icon-del-button"></i>
                      <span>删除</span>
                    </div>
                    <div class="btn-glow"></div>
                  </el-button>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="scifi-divider"></div>

        <!-- 分页 -->
        <div class="scifi-pagination-panel">
          <div class="pagination-info">
            共 {{ total }} 条记录
          </div>
          <div class="scifi-pagination">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="queryInfo.pageSize"
                layout="sizes, prev, pager, next"
                :total="total"
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </div>

    <el-dialog
        :title="dialogTitle"
        :visible.sync="noticeShow"
        width="500px"
        center
        @close="noticeClose"
    >
      <!-- 内容主题区域 -->
      <el-form
          :model="noticeForm"
          :rules="noticeRules"
          ref="noticeForm"
          label-width="80px"
      >
        <el-form-item label="公告标题" prop="title" class="is-required">
          <el-input
              v-model="noticeForm.title"
              style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="公告内容" prop="content" class="is-required">
          <el-input
              type="textarea"
              :rows="5"
              v-model="noticeForm.content"
              style="width: 300px"
          ></el-input>
        </el-form-item>
      </el-form>

      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
                <el-button @click="noticeClose()">取 消</el-button>
                <el-button type="primary" @click="noticeCommit('noticeForm')"
                           icon="iconfont icon-r-yes"> 确认提交</el-button
                >
            </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      total: 0,
      loading: true,
      dialogTitle: '',
      isEdit: false,
      queryInfo: {
        title: "",
        pagenum: 1,
        pageSize: 6,
      },
      notices: [],
      noticeShow: false,
      noticeForm: {
        id: '',
        title: '',
        content: ''
      },
      noticeRules: {
        title: [
          { required: true, message: "请输入公告标题", trigger: "blur" }
        ],
        content: [
          { required: true, message: "请输入公告内容", trigger: "blur" }
        ],
      }
    };
  },
  methods: {
    getList() {
      axios.post("/api/notice/pageNotice", this.queryInfo).then((res) => {
        this.loading = false;
        this.total = res.data.data.total;
        this.notices = res.data.data.records;
      });
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getList();
    },
    addNotice() {
      this.isEdit = false;
      this.dialogTitle = '新增公告';
      this.noticeShow = true;
    },
    toEdit(notice) {
      this.isEdit = true;
      this.dialogTitle = '编辑公告';
      this.noticeForm.id = notice.id;
      this.noticeForm.title = notice.title;
      this.noticeForm.content = notice.content;
      this.noticeShow = true;
    },
    noticeClose() {
      this.noticeShow = false;
      this.$nextTick(() => {
        this.$refs.noticeForm.resetFields();
        this.noticeForm = {
          id: '',
          title: '',
          content: ''
        };
      });
    },
    noticeCommit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = this.isEdit ? "/api/notice/update" : "/api/notice/save";
          axios.post(url, this.noticeForm).then((res) => {
            if (res.data.flag) {
              this.$message.success(this.isEdit ? "修改成功！" : "新增成功！");
              this.getList();
            } else {
              this.$message.error(this.isEdit ? "修改失败！" : "新增失败！");
            }
            this.noticeShow = false;
          });
        } else {
          return false;
        }
      });
    },
    async noticeDelete(id) {
      const re = await this.$confirm(
          "此操作将永久删除该公告, 是否继续?",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
      ).catch((err) => err);

      if (re != "confirm") {
        this.$message.info("已取消删除！");
      } else {
        axios.get("/api/notice/del?id=" + id).then((res) => {
          if (res.data.data) {
            this.$message.success("删除成功！");
            this.getList();
          } else {
            this.$message.error("删除失败！");
          }
        });
      }
    },
    //时间转换函数
    dateFormat(val, timeType) {
      //将字符串或长整型转为时间类型
      let date = new Date(val);
      //默认格式为'年月日 时:分:秒'
      let resultFormat = "Y年M月D日 h:m:s";
      switch (timeType) {
        case 1:
          resultFormat = "Y年M月D日";
          break;
        case 2:
          resultFormat = "Y-M-D";
          break;
        case 3:
          resultFormat = "Y-M-D h:m:s";
          break;
      }
      let result = formatTime(resultFormat);

      // 格式化日期，如月、日、时、分、秒保证为2位数
      function formatNumber(n) {
        n = n.toString();
        return n[1] ? n : "0" + n;
      }
      // 参数number为毫秒时间戳，format为需要转换成的日期格式
      function formatTime(format) {
        let newArr = [];
        let formatArr = ["Y", "M", "D", "h", "m", "s"];
        newArr.push(date.getFullYear());
        newArr.push(formatNumber(date.getMonth() + 1));
        newArr.push(formatNumber(date.getDate()));

        newArr.push(formatNumber(date.getHours()));
        newArr.push(formatNumber(date.getMinutes()));
        newArr.push(formatNumber(date.getSeconds()));

        for (let i in newArr) {
          format = format.replace(formatArr[i], newArr[i]);
        }
        return format;
      }

      return result;
    },
  },
  mounted() {
    this.getList();
  },
};
</script>

<style scoped lang="less">
.scifi-stall-container {
  position: relative;
  min-height: 100vh;
  background: linear-gradient(135deg, #0c0c2e 0%, #1a1a3e 50%, #2d2d5e 100%);
  overflow-x: hidden;
  font-family: 'Arial', sans-serif;
  color: #fff;
}

.scifi-content-wrapper {
  position: relative;
  z-index: 1;
  padding: 20px;
  display: flex;
  justify-content: center;
}

.scifi-card {
  position: relative;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  padding: 30px;
  width: 95%;
  max-width: 1200px;
  box-shadow:
      0 8px 32px rgba(0, 0, 0, 0.3),
      inset 0 1px 0 rgba(255, 255, 255, 0.2),
      0 0 20px rgba(0, 255, 255, 0.1);
  animation: glow 2s ease-in-out infinite alternate;
}

.scifi-header {
  text-align: center;
  margin-bottom: 30px;
}

.scifi-title {
  color: #00ffff;
  font-size: 28px;
  font-weight: 300;
  margin-bottom: 8px;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
  letter-spacing: 2px;
}

.scifi-subtitle {
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
  margin: 0;
}

.scifi-search-panel {
  margin-bottom: 20px;
}

.panel-header {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #00ffff;
  font-size: 20px;
  margin-bottom: 20px;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.3);
}

.search-controls {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 15px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 20px;
}

.search-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  align-items: end;
}

.search-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-icon {
  color: #00ffff;
  font-size: 18px;
  margin-bottom: 5px;
}

.scifi-input {
  :deep(.el-input__inner) {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 10px;
    color: #fff;
    height: 45px;
    transition: all 0.3s ease;

    &::placeholder {
      color: rgba(255, 255, 255, 0.5);
    }

    &:focus {
      border-color: #00ffff;
      box-shadow: 0 0 15px rgba(0, 255, 255, 0.3);
      background: rgba(255, 255, 255, 0.08);
    }
  }

  :deep(.el-select .el-input__inner) {
    padding-left: 15px;
  }
}

.search-btn {
  background: linear-gradient(135deg, #00ffff, #0080ff);
  color: #000;
  width: 100%;
  height: 45px;

  &:hover {
    box-shadow: 0 5px 20px rgba(0, 255, 255, 0.4);
  }
}

.order-btn {
  background: linear-gradient(135deg, #ffff00, #ff8000);
  color: #000;
  padding: 8px 12px;
  font-size: 12px;
  margin-right: 5px;

  &:hover {
    box-shadow: 0 5px 20px rgba(255, 255, 0, 0.4);
  }
}

.scifi-divider {
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(0, 255, 255, 0.5), transparent);
  margin: 25px 0;
}

.scifi-table-panel {
  margin-bottom: 20px;
}

.scifi-table-container {
  background: rgba(255, 255, 255, 0.02);
  border-radius: 15px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

.table-header {
  display: grid;
  grid-template-columns: 60px 1fr 1fr 1fr 1fr 200px;
  gap: 1px;
  background: rgba(0, 255, 255, 0.2);
  padding: 0;
}

.header-item {
  padding: 15px 10px;
  background: rgba(0, 255, 255, 0.1);
  color: #00ffff;
  font-weight: 500;
  text-align: center;
  font-size: 14px;
}

.table-body {
  max-height: 500px;
  overflow-y: auto;
}

.table-row {
  display: grid;
  grid-template-columns: 60px 1fr 1fr 1fr 1fr 200px;
  gap: 1px;
  background: rgba(255, 255, 255, 0.02);
  transition: all 0.3s ease;

  &:hover {
    background: rgba(255, 255, 255, 0.05);
    transform: translateX(5px);
  }

  &:nth-child(even) {
    background: rgba(255, 255, 255, 0.03);
  }
}

.table-cell {
  padding: 12px 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
}

.action-cell {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.index {
  color: rgba(255, 255, 255, 0.7);
  font-weight: 500;
}

.title {
  color: #00ffff;
  font-weight: 500;
}

.content, .create-time, .operator {
  color: rgba(255, 255, 255, 0.9);
}

/* 修复按钮对齐问题 */
.action-btn {
  min-width: 70px;
  height: 36px;
  padding: 8px 12px;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.edit-btn {
  background: linear-gradient(135deg, #ffff00, #ff8000);
  color: #000;
}

.delete-btn {
  background: linear-gradient(135deg, #ff00ff, #8000ff);
  color: #fff;
}

.action-btn:hover {
  transform: translateY(-2px);
}

.edit-btn:hover {
  box-shadow: 0 5px 20px rgba(255, 255, 0, 0.4);
}

.delete-btn:hover {
  box-shadow: 0 5px 20px rgba(255, 0, 255, 0.4);
}

.scifi-pagination-panel {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.pagination-info {
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
}

.scifi-pagination {
  :deep(.el-pagination) {
    .btn-prev, .btn-next, .number, .el-icon {
      background: rgba(255, 255, 255, 0.05);
      color: rgba(255, 255, 255, 0.8);
      border: 1px solid rgba(255, 255, 255, 0.2);
      border-radius: 6px;
      margin: 0 2px;
    }

    .number.active {
      background: linear-gradient(135deg, #00ffff, #0080ff);
      color: #000;
      border: 1px solid #00ffff;
    }

    .el-pagination__sizes .el-input .el-input__inner {
      background: rgba(255, 255, 255, 0.05);
      color: rgba(255, 255, 255, 0.8);
      border: 1px solid rgba(255, 255, 255, 0.2);
    }
  }
}

.no-data {
  padding: 60px 20px;
  text-align: center;
  background: rgba(255, 255, 255, 0.02);
}

.no-data-text {
  color: rgba(255, 255, 255, 0.5);
  font-size: 18px;
}

/* 按钮通用样式 */
.scifi-btn {
  position: relative;
  border: none;
  border-radius: 10px;
  padding: 12px 24px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  overflow: hidden;
  min-width: 70px;

  .btn-content {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .btn-glow {
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
    transition: left 0.5s ease;
  }

  &:hover {
    transform: translateY(-2px);

    .btn-glow {
      left: 100%;
    }
  }
}

/* 背景元素样式 */
.scifi-bg-elements {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.floating-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(40px);
  opacity: 0.1;
  animation: float 6s ease-in-out infinite;
}

.orb-1 {
  width: 200px;
  height: 200px;
  background: #00ffff;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.orb-2 {
  width: 150px;
  height: 150px;
  background: #ff00ff;
  top: 60%;
  right: 10%;
  animation-delay: 2s;
}

.orb-3 {
  width: 100px;
  height: 100px;
  background: #ffff00;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

.grid-lines {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image:
      linear-gradient(rgba(0, 255, 255, 0.1) 1px, transparent 1px),
      linear-gradient(90deg, rgba(0, 255, 255, 0.1) 1px, transparent 1px);
  background-size: 50px 50px;
  opacity: 0.3;
}

/* 动画 */
@keyframes glow {
  from {
    box-shadow:
        0 8px 32px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.2),
        0 0 20px rgba(0, 255, 255, 0.1);
  }
  to {
    box-shadow:
        0 8px 32px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.2),
        0 0 30px rgba(0, 255, 255, 0.3);
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) translateX(0);
  }
  50% {
    transform: translateY(-20px) translateX(10px);
  }
}

/* 滚动条样式 */
.table-body::-webkit-scrollbar {
  width: 6px;
}

.table-body::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.05);
}

.table-body::-webkit-scrollbar-thumb {
  background: rgba(0, 255, 255, 0.3);
  border-radius: 3px;
}

.table-body::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 255, 255, 0.5);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .table-header,
  .table-row {
    grid-template-columns: 50px 1fr 1fr 1fr 1fr 200px;
  }

  .header-item,
  .table-cell {
    font-size: 12px;
    padding: 10px 5px;
  }
}

@media (max-width: 768px) {
  .scifi-card {
    width: 95%;
    padding: 20px 15px;
  }

  .search-row {
    grid-template-columns: 1fr;
    gap: 15px;
  }

  .table-header,
  .table-row {
    grid-template-columns: 1fr;
    display: block;
  }

  .header-item {
    display: none;
  }

  .table-row {
    margin-bottom: 10px;
    padding: 15px;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 10px;
    border: 1px solid rgba(255, 255, 255, 0.1);

    &:hover {
      transform: translateY(-2px);
    }
  }

  .table-cell {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);

    &:last-child {
      border-bottom: none;
      justify-content: center;
      padding-top: 15px;
    }

    &::before {
      content: attr(data-label);
      color: #00ffff;
      font-weight: 500;
      font-size: 12px;
    }
  }

  .table-cell:nth-child(1)::before { content: "序号"; }
  .table-cell:nth-child(2)::before { content: "公告标题"; }
  .table-cell:nth-child(3)::before { content: "公告内容"; }
  .table-cell:nth-child(4)::before { content: "创建时间"; }
  .table-cell:nth-child(5)::before { content: "操作人"; }
  .table-cell:nth-child(6)::before { content: "操作"; }

  .action-cell {
    flex-direction: row;
    justify-content: center;
    gap: 10px;
    align-items: center;
  }

  .action-btn {
    min-width: 60px;
    height: 32px;
    padding: 6px 10px;
    font-size: 11px;
  }

  .btn-content {
    gap: 5px;
  }

  .scifi-pagination-panel {
    flex-direction: column;
    gap: 15px;
    text-align: center;
  }
}
</style>